package com.example.zhaicaoapplicationv3.BookShelf;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;

import com.example.zhaicaoapplicationv3.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import android.view.LayoutInflater;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.navigation.NavigationView;

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * 书架：实现底部导航栏的UI设计、菜单显示且监听功能、创建功能
 * @author lxx
 * 2019.10.19
 */
public class Bookshelf_Activity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bookshelf);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        /**
         * 监控加(+)号按钮的单击事件行为并作出处理
         * @author lxx
         * 2019.10.19
         */
        FloatingActionButton bt_plus2 = findViewById(R.id.bt_puls2);
        bt_plus2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Plusbutton_bookshelf(Bookshelf_Activity.this,"新建摘抄集").show();
            }
        });


        /**
         * 初始化固定的侧滑菜单中的Fragment页面并与侧滑菜单中对应的控件进行绑定
         * @author lxx
         * 2019.10.19
         */
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_weidu, R.id.nav_zaidu, R.id.nav_yidu,
                R.id.nav_free,R.id.nav_setting)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }


    /**
     * 显示页面右上角菜单
     * @author lxx
     * 2019.10.19
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bookshlf_, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
    //监听侧滑菜单

    /**
     * 显示创建摘抄集的对话框
     * @author lxx
     * 2019.10.19
     */
      public AlertDialog.Builder Plusbutton_bookshelf(final Context context, String title){
         LinearLayout loginDialog= (LinearLayout) LayoutInflater.from(context).inflate(R.layout.plusbutton_bookshelf,null);
         AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle(title)
                 //监听保存键的点击行为
                 .setView(loginDialog).setPositiveButton("保存", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialogInterface, int i) {
                         Toast.makeText(Bookshelf_Activity.this,"保存成功",Toast.LENGTH_SHORT).show();
                     }
                 })
                 .setNegativeButton("取消", null);
         return builder;
     }
    /**
     * 关闭Activity的退出动画效果
     * @author lxx
     * 2019.10.19
     */
    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }
}
